﻿Public Class ThreadHandler

    Dim _active_count As Integer = 0
    Dim _last_number As Integer
    Dim _list_controls As New List(Of Control)
    Dim _max_threads As Integer
    Dim _download_stopped As Boolean
    Dim _lock As New Object

    Public Sub HandleThreads(ByVal _var_list_controls As List(Of Control), ByVal _var_max_threads As Integer)

        _list_controls = _var_list_controls
        _max_threads = _var_max_threads

        Try
            SyncLock _lock

                For x = 0 To _list_controls.Count - 1

                    Dim _thread As Threading.Thread
                    Dim _control As New ctrFTPClient

                    If Not _active_count >= _max_threads Then

                        _control = CType(_list_controls(x), ctrFTPClient)

                        _thread = New Threading.Thread(AddressOf _control.Download)

                        Debug.WriteLine(String.Format("[FIRST RUN] Starte Thread! - {0} von Maximal {1} aktiven Threads", _active_count, _max_threads))

                        _thread.Start()

                        _active_count += 1

                        _last_number = x

                        System.Threading.Thread.Sleep(500)

                    Else
                        Debug.WriteLine(String.Format("Maximale Thread Anzahl erreicht! - Letztes Aktives Control (Index): {0}", _last_number))
                    End If

                Next

            End SyncLock

        Catch ex As Exception
            Debug.WriteLine("FATAL ERROR: " & ex.Message)
        End Try


    End Sub

    Public Sub DecrementActiveThreadCount()

        Try

            SyncLock _lock

                If Not _active_count = 0 Then
                    _active_count -= 1
                End If

                Debug.WriteLine(String.Format("Aktive Thread Anzahl dezimiert! - Aktueller Wert:{0} von Maximal:{1}", _active_count, _max_threads))

                If _download_stopped = False Then

                    For x = _last_number + 1 To _list_controls.Count - 1

                        Dim _thread As Threading.Thread
                        Dim _control As New ctrFTPClient

                        If Not _active_count >= _max_threads Then

                            _control = CType(_list_controls(x), ctrFTPClient)

                            _thread = New Threading.Thread(AddressOf _control.Download)

                            Debug.WriteLine(String.Format("Starte Thread! - {0} von Maximal {1} aktiven Threads", _active_count, _max_threads))

                            _thread.Start()

                            _active_count += 1

                            _last_number = x

                            System.Threading.Thread.Sleep(500)

                        Else
                            Debug.WriteLine(String.Format("Maximale Thread Anzahl erreicht! - Letztes Aktives Control (Index): {0}", _last_number))
                        End If

                    Next

                End If

            End SyncLock

        Catch ex As Exception
            Debug.WriteLine("FATAL ERROR: " & ex.Message)
        End Try

    End Sub

    Public ReadOnly Property GetActiveThreadCount As Integer
        Get
            Return _active_count
        End Get
    End Property

    Public WriteOnly Property SetDownloadStopped As Boolean
        Set(value As Boolean)
            _download_stopped = value
        End Set
    End Property

End Class
